package com.etop.jansing.util;


import com.etop.jansing.dto.ResourceDto;
import com.etop.jansing.entities.BaseEntity;
import com.etop.jansing.entities.Resource;
import com.etop.jansing.entities.ResourcePermission;
import com.etop.jansing.entities.Role;

import java.util.*;

/**
 * Created by jansing on 2015/10/19.
 */
public class CommonUtils {
    /**
     * 资源排序器，用于确定排列顺序
     */
    public static Comparator<ResourceDto> resourceComparator = initResourceComparator();

    public static final int RESOURCE_MAX_LAYER = 3;


    /**
     * 将页面传来的id转成long型返回，
     * 如果没有则返回null
     *
     * @param tempId
     * @return
     */
    public static Long getId(String tempId) {
        try {
            Long id = Long.parseLong(tempId);
            if (id != null && id > 0) {
                return id;
            } else return null;
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * @param ids 表单传来的一组id
     * @return
     */
    public static List<Long> parseIds(String[] ids) {
        if (ids == null || ids.length == 0) {
            return null;
        }
        List<Long> result = new ArrayList<>();
        for (String id : ids) {
            result.add(Long.parseLong(id));
        }
        return result;
    }

    public static List<Long> getIds(Collection<? extends BaseEntity> entities){
        if(entities==null||entities.size()==0){
            return null;
        }
        List<Long> ids = new ArrayList<>();
        for(BaseEntity entity : entities){
            ids.add(entity.getId());
        }
        return ids;
    }

    /**
     * 用于增改角色页面，<br/>
     * 将资源实体类转成dto，<br/>
     *
     * @param root
     * @param role
     * @return
     */
    public static ResourceDto getResourceDto(Resource root, Collection<ResourcePermission> resourcePermission) {
        ResourceDto rootDto = new ResourceDto(root, false);
        int count1 = root.getSubList().size();
        for (Resource r2 : root.getSubList()) {
            ResourceDto r2Dto = new ResourceDto(r2, false);
            //二级资源可以拥有权限，也可以拥有子资源
            if (r2.getSubList().size() > 0) {
                int count2 = r2.getSubList().size();
                for (Resource r3 : r2.getSubList()) {
                    //三级资源没有子资源，只有权限
                    ResourceDto r3Dto = new ResourceDto(r3, false);
                    if (resourcePermission!=null && !resourcePermission.isEmpty()
                            && resourcePermission.containsAll(r3.getResourcePermissionList())) {
                        r3Dto.setIsSelected(true);
                        count2--;
                    }
                    r2Dto.getSubResourceDto().add(r3Dto);
                }
                sortResourceDto(r2Dto.getSubResourceDto());
                if (count2 == 0) {
                    //该二级资源的所有子资源都被选中，则它也被选中
                    r2Dto.setIsSelected(true);
                    count1--;
                }
            } else if (resourcePermission!=null && !resourcePermission.isEmpty()
                    && resourcePermission.containsAll(r2.getResourcePermissionList())) {
                r2Dto.setIsSelected(true);
                count1--;
            }
            rootDto.getSubResourceDto().add(r2Dto);
        }
        sortResourceDto(rootDto.getSubResourceDto());
        if (count1 == 0) {
            rootDto.setIsSelected(true);
        }
        return rootDto;
    }

    /**
     * 按orderId给资源排序，用于确定显示时的排列顺序
     *
     * @param resourceDtoList
     */
    public static void sortResourceDto(List<ResourceDto> resourceDtoList) {
        Collections.sort(resourceDtoList, resourceComparator);
    }

    /**
     * 用于页面显示资源树，<br/>
     * 将资源dto按资源的orderId排序，<br/>
     *
     * @return
     */
    public static Comparator initResourceComparator() {
        return new Comparator<ResourceDto>() {
            public int compare(ResourceDto arg0, ResourceDto arg1) {
//                按orderId从小到大排序
                return arg0.getResource().getOrderId().compareTo(arg1.getResource().getOrderId());
//                按orderId从大到小排序
//                return arg1.getResource().getOrderId().compareTo(arg0.getResource().getOrderId());
            }
        };
    }

    /**
     * 用于生成资源增改的父资源，
     *
     * @param resourceList
     * @return
     */
    public static List<Resource> getCanBeParent(List<Resource> resourceList) {
        List<Resource> parent = new ArrayList<>();
        for (Resource r : resourceList) {
//            无权限且不为三层资源的才能成为父资源
            if (r.getResourcePermissionList().isEmpty() && getResourceLayer(r) < RESOURCE_MAX_LAYER) {
                parent.add(r);
            }
        }
        return parent;
    }

    public static int getResourceLayer(Resource resource) {
        if (resource.getParent() == null)
            return 1;
        if (resource.getParent().getParent() == null)
            return 2;
        return 3;
    }
}
